function Depart_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div id="find" class="finds"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search()" onkeyup="Depart_Search();"/><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div></div>');
	buffer.append('<div class="horizen border-bottom"></div>');
	return buffer.toString();
};

function Depart_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="customerlist border-bottom" id="'+info.big.ID+'" parentid="'+info.big.ParentID+'" headuserid="'+info.big.HeadUserID+'" headusername="'+info.big.HeadUserName+'"><div class="customername">'+info.big.Name+'</div><div class="customerinfo"><div class="state"></div><div class="lastconntime">负责人：'+(info.big.HeadUserName==""?"暂无":info.big.HeadUserName)+'</div></div></div>');
		$(info.small).each(function(i,small){
			buffer.append('<div class="customerlist border-bottom" id="'+small.ID+'" parentid="'+small.ParentID+'" headuserid="'+small.HeadUserID+'" headusername="'+small.HeadUserName+'"><div class="customername" style="padding-left:40px;">'+small.Name+'</div><div class="customerinfo"><div class="state"></div><div class="lastconntime">负责人：'+(small.HeadUserName==""?"暂无":small.HeadUserName)+'</div></div></div>');
		});
	});
	return buffer.toString();
};

function Receive_Depart(pageindex)
{
	var data = {};
			data.act = "Depart_Data";
			data.userid=userid;
			data.comid=comid;
			data.source=source;
			data.keyword = $("#keyword").val();
		    data.timer = "";
		    data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				if(json.data.departlist.length>0)
				{
					$("#contentlist1").html('<div id="nlist" style="float:left; min-height: '+sheight+'px;"></div>');
					$("#contentlist1").append(Depart_Data(json.data.departlist));
					$("#contentlist1").append('<div class="loading">长按可以删除哦</div>');
					setTimeout(function(){
						Init_Depart();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.departlist.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据"); 
					}
					else
					{
						$("#loading1").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};

function Depart_Search(){
	Receive_Depart(1);
};

function Init_Depart()
{
	mui("#contentlist1").off("tap",".customerlist");
	mui("#contentlist1").on("tap",".customerlist",function(){
		obj=$(this);
		OpenWindow(2,"部门","保存",function(){
			Save_Depart();
		});
		$("#contentlist2").html(DepartAdd_Code($(this).attr("id"),$(this).attr("parentid"),$(this).find(".customername").html(),$(this).attr("headuserid"),$(this).attr("headusername")));
		Init_DepartAdd();
	});
	mui("#contentlist1").off("longtap",".customerlist");
	mui("#contentlist1").on("longtap",".customerlist",function(){
		if($(this).attr("parentid")=="0")
		{
			plus.nativeUI.alert("公司不能删除");	
		}else
		{
			obj=$(this);
        	plus.nativeUI.confirm("确定要删除吗？", function(event){
				if(event.index==1)
				{
					var data = {};
				    data.act = "Delete_Depart";
				    data.id = obj.attr("id");
				    data.userid=userid;
					data.comid=comid;
					data.source=source;
					data.sign=GetSign(data.act);
				    mui.getJSON(admin_domain, data, function(json) {
						if(json.state)
						{
							obj.hide(300);
							plus.nativeUI.toast(json.msg);
						}else
						{
							plus.nativeUI.alert(json.msg);
						}
						
				    });	
				}
			},"提示", ["取消","确认"]); 
		}
	});
};
